using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace DNT.RavenQA.Web.Infrastructure.Registry
{
    public class IncludeNonPublicMembersContractResolver : Raven.Imports.Newtonsoft.Json.Serialization.DefaultContractResolver
    {
        public IncludeNonPublicMembersContractResolver()
        {
            DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
        }

        protected override List<MemberInfo> GetSerializableMembers(Type objectType)
        {
            return objectType.GetFields(FieldsBinding)
                             .Union(objectType.GetProperties(PropertiesBinding)
                             .Where(p => p.CanWrite).Cast<MemberInfo>())
                             .ToList();
        }

        private const BindingFlags FieldsBinding = BindingFlags.Instance | BindingFlags.Public;
        private const BindingFlags PropertiesBinding = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    }
}